if SERVER then
	registerCommand("ban", "Bans the player you specify. You may also optionally specify the amount of time (defaults to 60 mins) and the reason",function(param)
		local banReason = "caller banned target for: amount minutes for: reason"
		local banNoReason = "caller banned target for: amount minutes, reason not specified"
		if param["settings"]["ban_with_reason_msg"] then
			banReason = param["settings"]["ban_with_reason_msg"]
		end
		if param["settings"]["ban_without_reason_msg"] then
			banNoReason = param["settings"]["ban_without_reason_msg"]
		end
		local banTable = {}
		banTable["caller"] = getCaller(param[1])
		local targs = string.Explode(" ", param[3])
		local why = ""
		local dur = 60
		if not getPlayerByName(targs[table.Count(targs)]) then
			if not getPlayerByName(targs[table.Count(targs) - 1]) then
				why = targs[table.Count(targs)]
				table.remove(targs, table.Count(targs))
				dur = tonumber(table.remove(targs, table.Count(targs)))
			else
				dur = tonumber(targs[table.Count(targs)])
			end
			if not dur then
				return nil
			end
		end
		for i, targ in pairs(targs) do
			local plyTarg = getPlayerByName(targ)
			if plyTarg && targ != "" then
				if why then
					banTable["target"] = plyTarg:GetName()
					banTable["amount"] = dur
					if why != "" then
						banTable["reason"] = why
						moocowWriteBan(plyTarg, dur, param[1], why)
						display(param[1], "ban", { plyTarg }, parseDisplayMessage(banReason, banTable))
						game.ConsoleCommand("kickid " .. plyTarg:UserID() .. "\"BANNED FOR " .. tostring(math.ceil(dur)) .. " MINUTES: " .. why .. "\n")
					else
						moocowWriteBan(plyTarg, dur, param[1], "NONE")
						display(param[1], "ban", { plyTarg }, parseDisplayMessage(banNoReason, banTable))
						game.ConsoleCommand("kickid " .. plyTarg:UserID() .. "\"BANNED FOR " .. tostring(math.ceil(dur)) .. " MINUTES\n")
					end
				end
			end
		end
	end)
	
	/*
	BAN FILE EXAMPLE
	SteamID     Time     BanFrom(time)     Name     Banner  BannerID    Reason
	*/
	function moocowWriteBan(ply, dur, caller, reason)
		if not file.Exists("moocow/bans.txt") then
			file.Write("moocow/bans.txt", "")
		end
		local mess = file.Read("moocow/bans.txt")
		
		local line = ply:SteamID() .. "\\" .. tostring(dur) .. "\\" .. os.time() .. "\\" ..  ply:GetName() .. "\\" .. caller:GetName() .. "\\" .. caller:SteamID()
		if reason && reason != "" then
			line = line .. "\\" .. reason
		else
			line = line .. "\\" .. "NONE"
		end
		file.Write("moocow/bans.txt", mess .. "\n" .. line)
	end
	
	local function kickBannedOnJoin(ply, id)
		id = string.lower(id)
		if file.Exists("moocow/bans.txt") then
			local mess = file.Read("moocow/bans.txt")
			local newMess = ""
			local data = string.Explode("\n", mess)
			for i, ln in pairs(data) do
				if ln != "" then
					local parts = string.Explode("\\", ln)
					if table.Count(parts) == 7 && string.lower(parts[1]) == id then
						if parts[2] != 0 then
							if os.difftime(os.time(), parts[3]) <= parts[2] * 60 then
								newMess = newMess .. ln .. "\n"
								if parts[7] != "NONE" then
									game.ConsoleCommand("kickid " .. id .. "\"BANNED FOR " .. tostring(math.ceil(tonumber(os.difftime(parts[3] + parts[2] * 60, os.time())) / 60)) .. " MINUTES: " .. parts[7] .. "\n")
								else
									game.ConsoleCommand("kickid " .. id .. "\"BANNED FOR " .. tostring(math.ceil(tonumber(os.difftime(parts[3] + parts[2] * 60, os.time())) / 60)) .. " MINUTES\n")
								end
							end
						else
							newMess = newMess .. ln .. "\n"
							if parts[7] != "NONE" then
								game.ConsoleCommand("kickid " .. id .. "\"PERMA-BANNED: " .. parts[7] .. "\n")
							else
								game.ConsoleCommand("kickid " .. id .. "\"PERMA-BANNED\n")
							end
						end
					else
						newMess = newMess .. ln .. "\n"
					end
				end
			end
			file.Write("moocow/bans.txt", newMess)
		end
	end
	hook.Add("PlayerAuthed", "moocow_ban", kickBannedOnJoin)
end